package tasks.OpportunitiesBaseData.Filters;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import resources.tasks.VerifyFilterContentHelper;
import tasks.OpportunitiesBaseData.ContentRule;
import core.Configurations;
import core.ControlsMapping;
import core.controls.SelectBox;
import dbops.SQLOperator;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Verifier extends VerifyFilterContentHelper
{
	/**
	 * Script Name   : <b>VerifyTable</b>
	 * Generated     : <b>Oct 12, 2011 12:06:49 PM</b>
	 * Description   : Functional Test Script
	 * Original Host : WinNT Version 6.1  Build 7601 (S)
	 * 
	 * @since  2011/10/12
	 * @author angel.tsvetkov
	 * @throws SQLException 
	 */
	
	public ArrayList<String> getDBContent(String ruleFileName,String brandName) throws SQLException
	{
		ArrayList<String> dbContent = new ArrayList<String>();
		
		ContentRule rule = new ContentRule(ruleFileName);
		SQLOperator query = new SQLOperator();
		
		String ruleContent = rule.getRule();
		
		if(ruleFileName.equals("Product_Product.sql") ||
				ruleFileName.equals("Product_Sub_Brand_SPL.sql"))
		{
			ruleContent = ruleContent.replace("%BRAND%", brandName);
		}
		
		ArrayList<ArrayList<String>> dbContentTmp = query.executeSelect(ruleContent);
		
		for(ArrayList<String> row:dbContentTmp)
		{
			dbContent.add(row.get(0));
		}
		
		return dbContent;
	}
	
	public boolean verify(String ruleFileName, SelectBox filterListBox, String brandName)
	{
		boolean result = true;
		String taskReport = "";
		boolean errorFlag = true;
		String feElementWrapper = "";
		
		String[] frontEndContent = filterListBox.getContent();
		 
		ArrayList<String> frontEndContentWrapper = new ArrayList<String>();
		
		for(String feElement:frontEndContent)
		{
			frontEndContentWrapper.add(feElement);
		}

		ArrayList<String> dbContent = new ArrayList<String>();

		try {
			dbContent = getDBContent(ruleFileName, brandName);
			
			if(frontEndContentWrapper.size() != dbContent.size())
			{
				setErrorComment("The available options in the front end (" + frontEndContent.length + ") have diferent count than the available options in the database (" + dbContent.size() + ")");
				result = false;
			}
			
			for(String dbElement:dbContent)
			{
				errorFlag = true;
				
				for(String feElement:frontEndContentWrapper)
				{
					feElementWrapper = feElement;
					if(dbElement == null)
					{
						dbElement = "";
					}
					
					if(feElement == null)
					{
						feElement = "";
					}
					
					if(dbElement.trim().equals(feElement.trim()))
					{
						errorFlag = false;
						break;
					}
					errorFlag = true;
				}

				if(errorFlag)
				{
					setErrorComment("The DB value: \"" + dbElement + "\" CAN NOT be found in the front end element " + filterListBox.getDescriptiveName() + ".");
					result = false;
				}
				else
				{
					frontEndContentWrapper.remove(feElementWrapper);
				}
			}
			
			if(!frontEndContentWrapper.isEmpty())
			{
				for(String frontEndElement:frontEndContentWrapper)
				{
					setErrorComment("The Front End value: \"" + frontEndElement + "\" in " + filterListBox.getDescriptiveName() + " CAN NOT be found in the database!");
				}
			}

	
		} catch (SQLException e) {
			System.err.println("Reading of data from database failure!");
			result = false;
			e.printStackTrace();
		}

		setTaskResult("Task: Verification of Front End and Database values.", result, taskReport);
		
		return result;
	}
}

